package springframework.beans.factory.support;

import springframework.beans.BeansException;
import springframework.beans.factory.BeanFactory;
import springframework.beans.factory.config.BeanDefinition;

/**
 * @author lzp
 * @Date:2023/3/10
 * @description: 抽象bean工厂，定义模板方法
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

	@Override
	public Object getBean(String beanName) throws BeansException {
		// 先走单例拿
		Object bean = getSingleton(beanName);
		if (bean != null) {
			return bean;
		}
		// 没有则进行注册
		BeanDefinition beanDefinition = getBeanDefinition(beanName);
		return createBean(beanName, beanDefinition);
	}

	/**
	 * 获取bean实例
	 */
	public abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

	/**
	 * 创建bean实例
	 */
	public abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;

}
